﻿@model Free2AskWeb.Models.PostModel

@{
    ViewBag.Title = "Modify";
}

@section Styles {
    <link href="@Url.Content("~/Content/Ask.css")" rel="stylesheet" type="text/css" />
}

@section JavaScript
{
@*    <script type="text/javascript" src="@Url.Content("/Scripts/Ask.js")"></script>*@

    <script type="text/javascript" src="../Content/tinymce/tinymce.min.js"></script>

    <script type="text/javascript">
    tinyMCE.init({
        mode: "textareas",
        encoding: "xml"
    });
    $(document).ready(function () {
        //alert("hello");
        $("#txtTag").keypress(function (event) {
            //alert("txtTag onchange event");
            if (event.which == 32) {
                $("#taglist").append("<div class='tag'><div class='tagText'>" + $("#txtTag").val() + "</div><div class='removeTag'></div></div>");
                $(this).val("");
            }
        });

        $("#txtTag").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/Tag/GetTags",
                    type: "POST",
                    dataType: "json",
                    data: JSON.stringify({ keyword: request.term }),
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        var taglist = $.map(JSON.parse(data.replace(/'/g, '"')), function (t) {
                            //alert(item);
                            return { label: t.TagName, value: t.TagName };
                        });
                        response(taglist);
                    }
                });
            },
            minLength: 1,
            select: function (event, ui) {

            }
        });

        $("#saveForm").submit(function () {
            var tags = [];
            $(".tagText").each(function () {
                //alert($(this).text()+"!");
                tags.push($(this).text());
            });
            //alert($("#PostContent").val());

            //tinyMCE.triggerSave();
            //tinyMCE.get('Content').getContent();
            //tinyMCE.activeEditor.getContent();

            var postModel = {
                PostID: $("#PostID").val(),
                Title: $("#Title").val(),
                Content: tinyMCE.get('Content').getContent(),//$("#Content").val(),
                Tags: tags
            };

            //alert($("#Content").length);

            $.ajax({
                type: "post",
                url: $(this).attr("action"),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(
                    postModel
                ),
                success: function (result) {
                    //alert("OK! " + result);
                    window.location.replace("Details?questionID=" + result);
                },
                error: function (result) {
                    alert('error')
                }
            });
            return false;
        });
    });
    
    </script>
}

<h2>Modify</h2>
 <p>@using (@Html.BeginForm("Modify", "Question", FormMethod.Post, new { id="saveForm"}))
{
    @Html.Hidden("PostID", Model.PostID)
    @Html.Partial("_QuestionEditorPartial", Model)
   <input type="submit" value="Save" class="creat floatLeft" />
    
    @*<div id="postContainer">
        @Html.Hidden("PostID",Model.PostID)
    
      <div id="title">@if(Model.Title!=null){@Html.TextBox("Title",Model.Title)}</div>
      <div id="postText">
          @Html.RichTextareaFor("Content",@Model.Content)
      </div>
      <div id="author">@if(Model.AuthorName!=null){<h3>@Model.AuthorName</h3>} Author Info</div>
        <input type="submit" value="Save" />
    </div>*@
}

@using (@Html.BeginForm("Delete", "Question", FormMethod.Post, new { id="deleteForm"}))
{
    @Html.Hidden("PostID", Model.PostID)
    <input type="submit" value="Delete"  class="creat floatLeft" />
}

 </p>